package com.ybc.chinese.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ybc.chinese.R;
import com.ybc.chinese.activity.DetailActivity;
import com.ybc.chinese.bean.Home;
import com.ybc.chinese.utils.Contants;

/**
 * <p>首页底部列表<p/>
 * Created by Yubch on 17/2/18.
 */
public class HomeItemView extends LinearLayout implements View.OnClickListener {
    private Context mCtx;
    private TextView tvCircle;
    private TextView tvTitle;
    private String mTitle;

    public HomeItemView(Context context) {
        super(context);
        initView(context);
    }

    public HomeItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public HomeItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        mCtx = context;
        View view = LayoutInflater.from(context).inflate(R.layout.view_home_item, this);
        LinearLayout layoutRoot = (LinearLayout) view.findViewById(R.id.layout_root);
        layoutRoot.setOnClickListener(this);
        tvCircle = (TextView) view.findViewById(R.id.tv_circle);
        tvTitle = (TextView) view.findViewById(R.id.tv_title);
    }

    public void setData(Home home, int position) {
        mTitle = home.getTitle();
        tvCircle.setText(mTitle.substring(0, 1));
        tvCircle.setBackgroundResource(Contants.CIRCLE_BG[position % Contants.CIRCLE_BG.length]);
        tvTitle.setText(mTitle);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_root:
                DetailActivity.newIntent(mCtx, mTitle);
                break;
        }
    }
}
